package io.renren.modules.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.enums.AuditLevel;
import io.renren.common.enums.AuditState;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.generator.entity.AuditEntity;
import io.renren.modules.generator.entity.ClassEntity;
import io.renren.modules.generator.entity.RestEntity;
import io.renren.modules.generator.service.AuditService;
import io.renren.modules.generator.service.ClassService;
import io.renren.modules.generator.service.RestService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * @author guijing
 * @email sunlightcs@gmail.com
 * @date 2022-02-05 22:24:18
 */
@RestController
@RequestMapping("generator/rest")
public class RestTimeController {
    @Autowired
    private RestService restService;

    @Autowired
    private AuditService auditService;

    @Autowired
    private ClassService classService;

    @Autowired
    SysUserService sysUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = ShiroUtils.getUserId();
        params.put("userId", userId);
        PageUtils page = restService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{restId}")
    public R info(@PathVariable("restId") Integer restId) {
        RestEntity rest = restService.getById(restId);

        return R.ok().put("rest", rest);
    }

    /**
     * 信息
     */
    @RequestMapping("/getRestStrip/{restId}")
    public R getRestStrip(@PathVariable("restId") Integer restId) {
        String restStrip = restService.getRestStrip(restId);

        return R.ok().put("url", restStrip);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional
    public R save(@RequestBody RestEntity rest) {
        Date date = new Date();
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        String nickName = userEntity.getNickName();
        ClassEntity classEntity = classService.getById(userEntity.getClassId());
        Long id = classEntity.getTeacherId();
        if (id == null) {
            throw new RRException("该班级没有指定班主任,无法请假请联系老师");
        }
        SysUserEntity headMater = sysUserService.getById(id);
        rest.setRestName(nickName);
        rest.setRestTime(date);
        rest.setRestUserId(ShiroUtils.getUserId());
        restService.save(rest);
        AuditEntity auditEntity = new AuditEntity();
        auditEntity.setAuditLevel(AuditLevel.HEADMASTER.getLevel());
        auditEntity.setAuditPeopleId(headMater.getUserId());
        auditEntity.setAuditPeopleName(headMater.getNickName());
        auditEntity.setAuditRecordId(rest.getRestId());
        auditEntity.setAuditStatus(AuditState.AUDITING.getState());
        auditEntity.setAuditTime(date);
        auditEntity.setReason(rest.getRestReason());
        auditEntity.setRestId(userEntity.getUserId());
        auditEntity.setRestName(nickName);
        auditService.save(auditEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RestEntity rest) {
        restService.updateById(rest);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] restIds) {
        restService.removeByIds(Arrays.asList(restIds));

        return R.ok();
    }

    /**
     * 撤销请假
     *
     * @return
     */
    @Transactional
    @RequestMapping("/revoke")
    public R revoke(@RequestBody Integer id) {
        QueryWrapper<AuditEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("audit_record_id", id);
        AuditEntity auditEntity = auditService.getOne(wrapper);
        restService.removeById(id);
        AuditEntity audit = new AuditEntity();
        audit.setAuditId(auditEntity.getAuditId());
        //设置审核为撤销
        audit.setAuditStatus(AuditState.REVOKE.getState());
        auditService.updateById(audit);
        return R.ok();
    }

}
